﻿<cfsilent>

	<cfscript>

		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		
		taskProp = getProperty("serviceFactory").getBean("taskPropertyAdvice");

		response = structNew();
		structInsert(response, "success", false, true);

		taskId = event.getArg("TSK");
		taskType = event.getArg("TT");
		
		transaction {
			
			/* 载入教学任务 */
			switch(taskType){
					case 'C':
						sql = "SELECT
							tsk.tsk_id,
							tsk.task_prop
						FROM
							t_common_task tsk
						WHERE
							 tsk.tsk_id = :taskId ";
						break;
					case 'P':
						sql = "SELECT
							tsk.tsk_id,
							tsk.task_prop
						FROM
							t_task tsk
						WHERE
							 tsk.tsk_id = :taskId ";
						break;
					case 'R':
						sql = "SELECT
							tsk.tsk_id,
							tsk.task_prop
						FROM
							t_reopen_task tsk
						WHERE
							 tsk.tsk_id = :taskId ";
						break;
					case 'T':
						sql = "SELECT
							tsk.tsk_id,
							tsk.task_prop
						FROM
							t_sport_task tsk
						WHERE
							 tsk.tsk_id = :taskId ";
						break;
				}
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				rs_task = queryObj.execute(sql = sql).getResult();

			/* 载入排课任务 */
			sql = "SELECT  a.period_done,
						   a.period_need 
					FROM t_task_arrange a 
					WHERE a.task_id = :taskId ";
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
			rs_taskArrange= queryObj.execute(sql = sql).getResult();
				
			taskProp.parseProperty( rs_task.task_prop );
			
			
			if (yesNoFormat(event.getArg('status'))) {
				
				if (rs_taskArrange.period_done gte rs_taskArrange.period_need) {

					/* 锁定排课任务 */
					taskProp.setArrangeDone(true);
					
					structInsert(response, "success", true, true);
				}
				else {
					structInsert(response, "reason", "已排学时不足", true);
				}
				
			}
			else {
				
				/* 解锁排课任务 */
				taskProp.setArrangeDone(false);
				
				structInsert(response, "success", true, true);
			}
			
			switch(taskType){
					case 'C':
						sql = "	UPDATE
									t_common_task t
								SET
									t.task_prop = :taskProp 
								WHERE
									t.tsk_id = :taskId ";
						break;
					case 'P':
						sql = "	UPDATE
									t_task t
								SET
									t.task_prop = :taskProp 
								WHERE
									t.tsk_id = :taskId ";
						break;
					case 'R':
						sql = "	UPDATE
									t_reopen_task t
								SET
									t.task_prop = :taskProp 
								WHERE
									t.tsk_id = :taskId ";
						break;
					case 'T':
						sql = "	UPDATE
									t_sport_task t
								SET
									t.task_prop = :taskProp 
								WHERE
									t.tsk_id = :taskId ";
						break;
				}
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_varchar");
				queryObj.addParam(name ="taskProp", value=taskProp.stringProperty(), cfsqltype="cf_sql_varchar");
				queryObj.execute(sql = sql);
			
		}
		
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>